package com.rdc.soulmatemusicplayer.base;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rdc.soulmatemusicplayer.R;
import com.rdc.soulmatemusicplayer.application.App;
import com.rdc.soulmatemusicplayer.service.PlayService;
import com.rdc.soulmatemusicplayer.util.MusicUtils;

import java.util.ArrayList;
import java.util.List;

//bug:
//第一次进入APP得先点击底部的播放按钮才能点击列表里的歌单，否则闪退



public class MainActivity extends  BaseActivity
{
	private ViewPager vp_main_viewpager;
	private RadioGroup rg_main_radio;
	//定义一个用来放Fragment的集合
	private List<Fragment> fragments=new ArrayList<>();
	//用来放View的集合
	private List<View> buttons;

	private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();




	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (ContextCompat.checkSelfPermission(App.sContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(MainActivity.this, new
					String[]{ Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
		}

		//获取控件
		vp_main_viewpager = (ViewPager) findViewById(R.id.vp_local_recent);
		rg_main_radio = (RadioGroup) findViewById(R.id.rg_main_radio);

		LocalFragment localFragment= new LocalFragment();
		RecentPlayFragment recentFragment= new RecentPlayFragment();

		//添加碎片页面
		fragments.add(localFragment);
		fragments.add(recentFragment);

		//设置适配器
		vp_main_viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));

		//得到单选组的所有单选按钮
		buttons=rg_main_radio.getTouchables();

		//给页面设置一个监听事件
		vp_main_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
		{
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
			{

			}

			//页面滑动事件
			@Override
			public void onPageSelected(int position) {
				//根据页面的位置拿到每个页面对应的单选按钮
				RadioButton radioButton= (RadioButton) buttons.get(position);
				//将该单选按钮设置为选中状态
				radioButton.setChecked(true);
			}

			@Override
			public void onPageScrollStateChanged(int state)
			{

			}
		});

		//给单选组设置点击事件监听
		rg_main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(RadioGroup group, @IdRes int checkedId)
			{
				//如果当前单选按钮的ID为自己设置的ID
				if(checkedId==R.id.rb_local)
				{
					//设置当前展示的页面
					vp_main_viewpager.setCurrentItem(0);
				} else if(checkedId==R.id.rb_recent)
						{
					vp_main_viewpager.setCurrentItem(1);

						}
			}
		});
	}



	//ViewPager的Adapter
	class MyAdapter extends FragmentPagerAdapter
	{

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}
	}


	/**
	 * 获取音乐播放服务
	 * @return
	 */
	public PlayService getPlayService()
	{
		return mPlayService;
	}



}

